package com.example.ad_project;

import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.nus.iss.ad.dbhelper.OrderDBHelper;


public class ConfirmDeliveryActivity extends BaseActivity
implements OnItemSelectedListener
{
	OrderDBHelper data=new OrderDBHelper();
	
	final static int CONDRIM_DELIVERY_DETAIL_REQUEST = 909;
	final static String PLEASE_SELECT="Please Select";
	List<String> valueList;
	
	private Spinner spinner1;
	
	String[] arr;
	
	 //@SuppressLint("NewApi")
	@Override
	  public void onCreate(Bundle savedInstanceState) {
		 
		 super.onCreate(savedInstanceState);
		 StrictMode.setThreadPolicy(ThreadPolicy.LAX);
		 setContentView(R.layout.confirm_delivery);

		spinner1 = (Spinner)findViewById(R.id.spinner1);
		
		int[] items=data.getPOIDList();
		arr=new String[items.length+1];
		arr[0]=PLEASE_SELECT;
		for(int i=0;i<items.length;i++){
		arr[i+1]= (new Integer(items[i])).toString();
	
	}
		
		valueList = Arrays.asList(arr);
		

		ArrayAdapter<String> adapter=
				new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,arr);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner1.setAdapter(adapter);
		spinner1.setSelection(0);
		spinner1.setOnItemSelectedListener(this);
	  }
	 
	 
	
	
	public void onQRScan(View v) {
		//Toast.makeText(getApplicationContext(),"Close PO Clicked",Toast.LENGTH_SHORT).show();
		
		//Toast.makeText(getApplicationContext(),Boolean.toString(isAllItemChecked()),Toast.LENGTH_SHORT).show();
		
		try {

			Intent intent = new Intent("com.google.zxing.client.android.SCAN");
			intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for
															// bar codes

			startActivityForResult(intent, 0);

		} catch (Exception e) {

			Uri marketUri = Uri
					.parse("market://details?id=com.google.zxing.client.android");
			Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
			startActivity(marketIntent);

		}
        
		
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {           
	    super.onActivityResult(requestCode, resultCode, data);
	    
	    if (resultCode == RESULT_OK && requestCode == CONDRIM_DELIVERY_DETAIL_REQUEST) {
	    	// refresh by restarting
	    		spinner1.setSelection(0);
	    	recreate();
	    } else if (requestCode == 0) {

	        if (resultCode == RESULT_OK) {
	            
	        	String contents = data.getStringExtra("SCAN_RESULT");
	            Log.d("QR test", contents);
	            if(valueList.contains(contents)){
	            	
	            	//orderData.closePO(orderId);
	            	//showDialog("Close Purchase Order successful",true);
	        		//setResult(RESULT_OK);
	                //finish();
	            	
	            	Intent intent = new Intent(this, ConfirmDeliveryDetail.class);
	 		       intent.putExtra("orderId", contents);
	 		       startActivityForResult(intent, CONDRIM_DELIVERY_DETAIL_REQUEST);
	            	
	            }else {
	            	Toast.makeText(getApplicationContext(),"Qr Code Invalid ",Toast.LENGTH_LONG).show();
				}
	            
	        }
	        if(resultCode == RESULT_CANCELED){
	            //handle cancel
	        	Toast.makeText(getApplicationContext(),"Please Scan QR code in Purchase Order to complete ",Toast.LENGTH_LONG).show();
	        }
	    }
	}
	
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			
               String orderId=String.valueOf(spinner1.getSelectedItem());
               
               if(orderId==PLEASE_SELECT)
               {return;}
               
		       Intent intent = new Intent(this, ConfirmDeliveryDetail.class);
		       intent.putExtra("orderId", orderId);
		       startActivityForResult(intent, CONDRIM_DELIVERY_DETAIL_REQUEST);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			Toast.makeText(this,
					"onNothingSelected : " + 
			                "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem())+ 
						Toast.LENGTH_SHORT, 0).show();
			
		}
		
		
		// @SuppressLint("NewApi")
		/*protected void onActivityResult
         (int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == CONDRIM_DELIVERY_DETAIL_REQUEST) {
// refresh by restarting
	spinner1.setSelection(0);
recreate();
			}
		}
	}*/
	
}
